package com.example.study.config;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;


/**
 * GlobalExceptionHandler 全局异常处理类，然后加上 @ControllerAdvice 注解即可拦截项目中抛出的异常，如下：
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {
    // 打印log
    public static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    // 、、、、、、


    /**
     * 拦截自定义业务异常，返回业务异常信息
     * @param ex z
     * @return z
     */
    @ExceptionHandler(BusinessErrorException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<String> handleBusinessError(BusinessErrorException ex) {
        String code = ex.getCode();
        String message = ex.getMessage();
        logger.error("业务异常:",ex);
        return new JsonResult<>(code, message);
    }



    /**
     * 缺少请求参数异常
     * @param ex HttpMessageNotReadableException
     * @return z
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public JsonResult<String> handleHttpMessageNotReadableException(MissingServletRequestParameterException ex){
        logger.error("缺少请求参数:",ex);
        return new JsonResult<>("400","缺少必要的参数");
    }

    @ExceptionHandler(java.sql.SQLIntegrityConstraintViolationException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<String> handleSQLIntegrityConstraintViolationException(Exception ex){
        logger.error("数据库异常:",ex);
        return new JsonResult<>("400","数据库异常: "+ex.getMessage());
    }
    @ExceptionHandler(org.springframework.dao.DuplicateKeyException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<String> handleSQLIntegrityConstraintViolationException22(java.sql.SQLIntegrityConstraintViolationException ex){
        logger.error("数据库异常222:",ex);
        return new JsonResult<>("400","数据库异常: "+ex.getMessage());
    }

    /**
     * 空指针异常
     * @param ex NullPointerException
     * @return z
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<String> handleTypeMismatchException(NullPointerException ex) {
        logger.error("空指针异常:", ex);
        return new JsonResult<>("500", "空指针异常了");
    }


    /**
     * 系统异常 预期以外异常
     * @param ex  但是项目中，我们一般都会比较详细的去拦截一些常见异常，拦截 Exception 虽然可以一劳永逸，
     *            但是不利于我们去排查或者定位问题。实际项目中，可以把拦截 Exception 异常写在 GlobalExceptionHandler
     *            最下面，如果都没有找到，最后再拦截一下 Exception 异常，保证输出信息友好。
     * @return z
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public JsonResult<String> handleUnexpectedServer(Exception ex) {
        logger.error("系统异常：",ex);
        return new JsonResult<>("500", "系统发生异常，请联系管理员"+"\n具体:"+ex.getMessage());
    }


}
